// user/order/complate.js
var home = require("../../utils/util.js");
var url = home.home;
var url2 = home.home2;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    onPayChheck1: false,
    onPayChheck2: false,
    onPayChheck3: true,
    payType: 3, //支付方式1会员余额  2股本余额  3微信支付
    userMoney: home.getKey('balance'),
    countPrice: 50,
    gbMoney: home.getKey('equityBalance'),
    stockLowest: home.getKey('noUseEquityBalancd'),
    goodsData: [],
    patternPayment: '03',
    balanceAmount: 0,
    stockBalanceAmount: 0,
    newDate:'',
    btnState:true,
    bgcolor: home.getKey('bgcolor'),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let goodsData = JSON.parse(decodeURIComponent(options.goodsData));
    console.log(goodsData);
    this.setData({
      goodsData: goodsData,
    });
  },

  getpOrderInfo(){
    let that = this;
    let goodsData = this.data.goodsData;
    wx.request({
      url: url + 'app/porder/detail?id='+goodsData.id,//新加接口查询充值和升级股东的订单
      data:{
        id:goodsData.id,
      },
      method: 'GET',
      header: {
        "content-type": "application/json", // 默认
      },
      success:function (res) {
        if (res.data.code == 200) {
          let newDate = '';
          let now = Math.floor(new Date(goodsData.createTime.replace(/-/g,"/")).getTime() / 1000);
          newDate = now + 15 * 60;
          that.setData({
            goodsData: res.data.result,
            countPrice: goodsData.rechargeAmount,//realAmount
            newDate:newDate
          });
          that.countTime();
        }
      }
    })
  },

  //获取用户数据
  getUserInfos(){
    let that = this;
    wx.request({
      url: url + 'app/getUserInfo',
      data:{
        storeId:home.getKey('storeId'),
        phone:home.getKey('phone'),
        xcxOpenId:home.getKey('openId'),
      },
      method: 'GET',
      header: {
        "content-type": "application/json", // 默认
      },
      success:function (res) {
        if (res.data.code == 200) {
          if (res.data.result.balance == null) {
            res.data.result.balance = 0;
          }
          if (res.data.result.equityBalance == null) {
            res.data.result.equityBalance = 0;
          }
          if (res.data.result.noUseEquityBalancd == null) {
            res.data.result.noUseEquityBalancd = 0;
          }
          that.setData({
            userMoney:res.data.result.balance,
            gbMoney:res.data.result.equityBalance,
            stockLowest:res.data.result.noUseEquityBalancd
          });
          home.setKey('equityBalance',res.data.result.equityBalance);
          home.setKey('balance',res.data.result.balance);
          home.setKey('noUseEquityBalancd',res.data.result.noUseEquityBalancd);
        }
      }
    })
  },

  //倒计时
  countTime() {
    let now = new Date().getTime();
    let newDate = this.data.newDate * 1000;
    let leftTime = newDate - now;
    let minutes, seconds;
    let that = this;
    if (leftTime >= 0) {
      minutes = Math.floor(leftTime / 1000 / 60 % 60);
      seconds = Math.floor(leftTime / 1000 % 60);
      seconds = seconds < 10 ? "0" + seconds : seconds
      minutes = minutes < 10 ? "0" + minutes : minutes
      that.setData({
        time: minutes + "分" + seconds + "秒",
      })
      //递归每秒调用countTime方法，显示动态时间效果
      setTimeout(that.countTime, 1000);
    } else {
      that.setData({
        time: '无法支付'
      })
    }
  },
  //选择支付方式
  onPayChheck(e) {
    let payType = e.currentTarget.dataset.paytype;
    let onPayChheck1 = false;
    let onPayChheck2 = false;
    let onPayChheck3 = false;
    let patternPayment = "03";
    if (payType == 1) {
      onPayChheck1 = true;
      patternPayment = '06';
    } else if (payType == 2) {
      onPayChheck2 = true;
      patternPayment = '05';
    } else {
      onPayChheck3 = true;
    }
    this.setData({
      onPayChheck1: onPayChheck1,
      onPayChheck2: onPayChheck2,
      onPayChheck3: onPayChheck3,
      patternPayment: patternPayment,
      payType: payType
    });
  },

  //跳转订单完成页面
  toAgainst() {
    //更新余额
    wx.request({
      url: url + 'app/getUserInfo',
      data:{
        storeId:home.getKey('storeId'),
        phone:home.getKey('phone'),
        xcxOpenId:home.getKey('openId'),
      },
      method: 'GET',
      header: {
        "content-type": "application/json", // 默认
      },
      success:function (res) {
        if (res.data.code == 200) {
          wx.redirectTo({
            //判断是充值还是升级股东跳转不同页：toback(1、充值 2、升级股东)
            url: './balance?balance=' + res.data.result.balance,
          })
        }
      },
      fail: function (res) {

      }
    })
  },

  //微信
  pay() {
    wx.showLoading({
      title: '加载中...',
    });
    this.setData({
      btnState: false
    });
    let patternPayment = this.data.patternPayment;
    if (patternPayment == '03') {
      this.xcxPay();
    } else {
      this.payOrder();
    }
  },

  xcxPay() {
    let that = this;
    console.log(that.data)
    wx.requestPayment({
      "timeStamp": that.data.goodsData.payParams.timeStamp,
      "nonceStr": that.data.goodsData.payParams.nonceStr,
      "package": that.data.goodsData.payParams.package_str,
      "signType": that.data.goodsData.payParams.signType,
      "paySign": that.data.goodsData.payParams.paySign,
      "success": function (res) {
        if (res.errMsg == 'requestPayment:ok') {
          wx.showToast({
            title: '支付完成',
            icon: 'success'
          });
          that.payOrder();
        }
      },
      "fail": function (res) {
        wx.hideLoading({
          success: (res) => {
            that.setData({
              btnState:true
            })
          },
        })
        wx.showToast({
          title: '取消支付',
          icon: 'none'
        });
      },
      "complete": function (res) {}
    })
  },

  payOrder() {
    let that = this;
    let payType = '微信支付';
    wx.request({
      url: url + 'app/depositOrder',//判断是充值还是升级股东，更新订单状态
      data: {
        orderId: that.data.goodsData.id,
        payType: payType,
      },
      method: 'POST',
      header: {
        "content-type": "application/json", // 默认
      },
      success: function (res) {
        if (res.data.code == 200) {
          that.toAgainst();
        } else {
          wx.hideLoading({
            success: (res) => {
              that.setData({
                btnState:true
              })
            },
          })
          wx.showToast({
            title: '支付失败',
            icon: 'none'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userMoney: home.getKey('balance'),
      gbMoney: home.getKey('equityBalance'),
      stockLowest: home.getKey('noUseEquityBalancd'),
    });
    this.getUserInfos();
    if(this.data.goodsData['name']=='会员余额充值'){
      this.getpOrderInfo();
    }else{
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})